package com.ugoodtech.mdcc.core.model.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.ugoodtech.mdcc.core.model.entity.Stations;
import com.ugoodtech.mdcc.core.model.entity.StationsExample.Criteria;
import com.ugoodtech.mdcc.core.model.entity.StationsExample.Criterion;
import com.ugoodtech.mdcc.core.model.entity.StationsExample;
import java.util.List;
import java.util.Map;

public class StationsSqlProvider {

    public String countByExample(StationsExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("stations");
        applyWhere(example, false);
        return SQL();
    }

    public String deleteByExample(StationsExample example) {
        BEGIN();
        DELETE_FROM("stations");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(Stations record) {
        BEGIN();
        INSERT_INTO("stations");
        
        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            VALUES("creation_time", "#{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            VALUES("deleted", "#{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            VALUES("enabled", "#{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAddress() != null) {
            VALUES("address", "#{address,jdbcType=VARCHAR}");
        }
        
        if (record.getCategory() != null) {
            VALUES("category", "#{category,jdbcType=VARCHAR}");
        }
        
        if (record.getCheckStation() != null) {
            VALUES("check_station", "#{checkStation,jdbcType=BIT}");
        }
        
        if (record.getCity() != null) {
            VALUES("city", "#{city,jdbcType=VARCHAR}");
        }
        
        if (record.getColorreportid() != null) {
            VALUES("colorReportId", "#{colorreportid,jdbcType=BIGINT}");
        }
        
        if (record.getContact() != null) {
            VALUES("contact", "#{contact,jdbcType=VARCHAR}");
        }
        
        if (record.getDistrict() != null) {
            VALUES("district", "#{district,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            VALUES("name", "#{name,jdbcType=VARCHAR}");
        }
        
        if (record.getProvince() != null) {
            VALUES("province", "#{province,jdbcType=VARCHAR}");
        }
        
        if (record.getStationNum() != null) {
            VALUES("station_num", "#{stationNum,jdbcType=VARCHAR}");
        }
        
        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerStat() != null) {
            VALUES("customer_stat", "#{customerStat,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerType() != null) {
            VALUES("customer_type", "#{customerType,jdbcType=BIGINT}");
        }
        
        if (record.getProductPosition() != null) {
            VALUES("product_position", "#{productPosition,jdbcType=VARCHAR}");
        }
        
        if (record.getPaintBrand() != null) {
            VALUES("paint_brand", "#{paintBrand,jdbcType=VARCHAR}");
        }
        
        if (record.getIsPaint() != null) {
            VALUES("is_paint", "#{isPaint,jdbcType=INTEGER}");
        }
        
        if (record.getIsOtherBrand() != null) {
            VALUES("is_other_brand", "#{isOtherBrand,jdbcType=BIT}");
        }
        
        if (record.getReportFinished() != null) {
            VALUES("report_finished", "#{reportFinished,jdbcType=INTEGER}");
        }
        
        if (record.getBrand() != null) {
            VALUES("brand", "#{brand,jdbcType=LONGVARCHAR}");
        }
        
        return SQL();
    }

    public String selectByExampleWithBLOBs(StationsExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("creation_time");
        SELECT("deleted");
        SELECT("enabled");
        SELECT("update_time");
        SELECT("address");
        SELECT("category");
        SELECT("check_station");
        SELECT("city");
        SELECT("colorReportId");
        SELECT("contact");
        SELECT("district");
        SELECT("name");
        SELECT("province");
        SELECT("station_num");
        SELECT("user_id");
        SELECT("customer_stat");
        SELECT("customer_type");
        SELECT("product_position");
        SELECT("paint_brand");
        SELECT("is_paint");
        SELECT("is_other_brand");
        SELECT("report_finished");
        SELECT("brand");
        FROM("stations");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String selectByExample(StationsExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("creation_time");
        SELECT("deleted");
        SELECT("enabled");
        SELECT("update_time");
        SELECT("address");
        SELECT("category");
        SELECT("check_station");
        SELECT("city");
        SELECT("colorReportId");
        SELECT("contact");
        SELECT("district");
        SELECT("name");
        SELECT("province");
        SELECT("station_num");
        SELECT("user_id");
        SELECT("customer_stat");
        SELECT("customer_type");
        SELECT("product_position");
        SELECT("paint_brand");
        SELECT("is_paint");
        SELECT("is_other_brand");
        SELECT("report_finished");
        FROM("stations");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        Stations record = (Stations) parameter.get("record");
        StationsExample example = (StationsExample) parameter.get("example");
        
        BEGIN();
        UPDATE("stations");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{record.deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{record.enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAddress() != null) {
            SET("address = #{record.address,jdbcType=VARCHAR}");
        }
        
        if (record.getCategory() != null) {
            SET("category = #{record.category,jdbcType=VARCHAR}");
        }
        
        if (record.getCheckStation() != null) {
            SET("check_station = #{record.checkStation,jdbcType=BIT}");
        }
        
        if (record.getCity() != null) {
            SET("city = #{record.city,jdbcType=VARCHAR}");
        }
        
        if (record.getColorreportid() != null) {
            SET("colorReportId = #{record.colorreportid,jdbcType=BIGINT}");
        }
        
        if (record.getContact() != null) {
            SET("contact = #{record.contact,jdbcType=VARCHAR}");
        }
        
        if (record.getDistrict() != null) {
            SET("district = #{record.district,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            SET("name = #{record.name,jdbcType=VARCHAR}");
        }
        
        if (record.getProvince() != null) {
            SET("province = #{record.province,jdbcType=VARCHAR}");
        }
        
        if (record.getStationNum() != null) {
            SET("station_num = #{record.stationNum,jdbcType=VARCHAR}");
        }
        
        if (record.getUserId() != null) {
            SET("user_id = #{record.userId,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerStat() != null) {
            SET("customer_stat = #{record.customerStat,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerType() != null) {
            SET("customer_type = #{record.customerType,jdbcType=BIGINT}");
        }
        
        if (record.getProductPosition() != null) {
            SET("product_position = #{record.productPosition,jdbcType=VARCHAR}");
        }
        
        if (record.getPaintBrand() != null) {
            SET("paint_brand = #{record.paintBrand,jdbcType=VARCHAR}");
        }
        
        if (record.getIsPaint() != null) {
            SET("is_paint = #{record.isPaint,jdbcType=INTEGER}");
        }
        
        if (record.getIsOtherBrand() != null) {
            SET("is_other_brand = #{record.isOtherBrand,jdbcType=BIT}");
        }
        
        if (record.getReportFinished() != null) {
            SET("report_finished = #{record.reportFinished,jdbcType=INTEGER}");
        }
        
        if (record.getBrand() != null) {
            SET("brand = #{record.brand,jdbcType=LONGVARCHAR}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExampleWithBLOBs(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("stations");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        SET("deleted = #{record.deleted,jdbcType=BIT}");
        SET("enabled = #{record.enabled,jdbcType=BIT}");
        SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        SET("address = #{record.address,jdbcType=VARCHAR}");
        SET("category = #{record.category,jdbcType=VARCHAR}");
        SET("check_station = #{record.checkStation,jdbcType=BIT}");
        SET("city = #{record.city,jdbcType=VARCHAR}");
        SET("colorReportId = #{record.colorreportid,jdbcType=BIGINT}");
        SET("contact = #{record.contact,jdbcType=VARCHAR}");
        SET("district = #{record.district,jdbcType=VARCHAR}");
        SET("name = #{record.name,jdbcType=VARCHAR}");
        SET("province = #{record.province,jdbcType=VARCHAR}");
        SET("station_num = #{record.stationNum,jdbcType=VARCHAR}");
        SET("user_id = #{record.userId,jdbcType=BIGINT}");
        SET("customer_stat = #{record.customerStat,jdbcType=BIGINT}");
        SET("customer_type = #{record.customerType,jdbcType=BIGINT}");
        SET("product_position = #{record.productPosition,jdbcType=VARCHAR}");
        SET("paint_brand = #{record.paintBrand,jdbcType=VARCHAR}");
        SET("is_paint = #{record.isPaint,jdbcType=INTEGER}");
        SET("is_other_brand = #{record.isOtherBrand,jdbcType=BIT}");
        SET("report_finished = #{record.reportFinished,jdbcType=INTEGER}");
        SET("brand = #{record.brand,jdbcType=LONGVARCHAR}");
        
        StationsExample example = (StationsExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("stations");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        SET("deleted = #{record.deleted,jdbcType=BIT}");
        SET("enabled = #{record.enabled,jdbcType=BIT}");
        SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        SET("address = #{record.address,jdbcType=VARCHAR}");
        SET("category = #{record.category,jdbcType=VARCHAR}");
        SET("check_station = #{record.checkStation,jdbcType=BIT}");
        SET("city = #{record.city,jdbcType=VARCHAR}");
        SET("colorReportId = #{record.colorreportid,jdbcType=BIGINT}");
        SET("contact = #{record.contact,jdbcType=VARCHAR}");
        SET("district = #{record.district,jdbcType=VARCHAR}");
        SET("name = #{record.name,jdbcType=VARCHAR}");
        SET("province = #{record.province,jdbcType=VARCHAR}");
        SET("station_num = #{record.stationNum,jdbcType=VARCHAR}");
        SET("user_id = #{record.userId,jdbcType=BIGINT}");
        SET("customer_stat = #{record.customerStat,jdbcType=BIGINT}");
        SET("customer_type = #{record.customerType,jdbcType=BIGINT}");
        SET("product_position = #{record.productPosition,jdbcType=VARCHAR}");
        SET("paint_brand = #{record.paintBrand,jdbcType=VARCHAR}");
        SET("is_paint = #{record.isPaint,jdbcType=INTEGER}");
        SET("is_other_brand = #{record.isOtherBrand,jdbcType=BIT}");
        SET("report_finished = #{record.reportFinished,jdbcType=INTEGER}");
        
        StationsExample example = (StationsExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(Stations record) {
        BEGIN();
        UPDATE("stations");
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAddress() != null) {
            SET("address = #{address,jdbcType=VARCHAR}");
        }
        
        if (record.getCategory() != null) {
            SET("category = #{category,jdbcType=VARCHAR}");
        }
        
        if (record.getCheckStation() != null) {
            SET("check_station = #{checkStation,jdbcType=BIT}");
        }
        
        if (record.getCity() != null) {
            SET("city = #{city,jdbcType=VARCHAR}");
        }
        
        if (record.getColorreportid() != null) {
            SET("colorReportId = #{colorreportid,jdbcType=BIGINT}");
        }
        
        if (record.getContact() != null) {
            SET("contact = #{contact,jdbcType=VARCHAR}");
        }
        
        if (record.getDistrict() != null) {
            SET("district = #{district,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            SET("name = #{name,jdbcType=VARCHAR}");
        }
        
        if (record.getProvince() != null) {
            SET("province = #{province,jdbcType=VARCHAR}");
        }
        
        if (record.getStationNum() != null) {
            SET("station_num = #{stationNum,jdbcType=VARCHAR}");
        }
        
        if (record.getUserId() != null) {
            SET("user_id = #{userId,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerStat() != null) {
            SET("customer_stat = #{customerStat,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerType() != null) {
            SET("customer_type = #{customerType,jdbcType=BIGINT}");
        }
        
        if (record.getProductPosition() != null) {
            SET("product_position = #{productPosition,jdbcType=VARCHAR}");
        }
        
        if (record.getPaintBrand() != null) {
            SET("paint_brand = #{paintBrand,jdbcType=VARCHAR}");
        }
        
        if (record.getIsPaint() != null) {
            SET("is_paint = #{isPaint,jdbcType=INTEGER}");
        }
        
        if (record.getIsOtherBrand() != null) {
            SET("is_other_brand = #{isOtherBrand,jdbcType=BIT}");
        }
        
        if (record.getReportFinished() != null) {
            SET("report_finished = #{reportFinished,jdbcType=INTEGER}");
        }
        
        if (record.getBrand() != null) {
            SET("brand = #{brand,jdbcType=LONGVARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(StationsExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}